package coml.jiyun.hp.ershi;


import java.util.List;

import coml.jiyun.hp.ershi.bean.WanBeanTwo;
import coml.jiyun.hp.ershi.db.DaoMaster;
import coml.jiyun.hp.ershi.db.DaoSession;
import coml.jiyun.hp.ershi.db.WanBeanTwoDao;

public class DbUtil {

    private static DbUtil dbUtil;
        private static WanBeanTwoDao wanBeanTwoDao;
    private static WanBeanTwo wanBeanTwo;

    public static  DbUtil dbUtil(){
            if (dbUtil == null){
                synchronized (DbUtil.class){
                    if (dbUtil==null){
                        dbUtil = new DbUtil();
                    }
                }
            }
            return dbUtil;
        }

        public DbUtil(){
            DaoMaster.DevOpenHelper devOpenHelper = new DaoMaster.DevOpenHelper(BaseApp.getInstance(), "student.db");

            DaoMaster daoMaster = new DaoMaster(devOpenHelper.getWritableDatabase());

            DaoSession daoSession = daoMaster.newSession();

           wanBeanTwoDao = daoSession.getWanBeanTwoDao();

        }
        public  void insert(WanBeanTwo wanBeanTwo){
            if (isHas(wanBeanTwo)){
                return;
            }
            wanBeanTwoDao.insertOrReplace(wanBeanTwo);
        }


    public List<WanBeanTwo> query(){
            return wanBeanTwoDao.queryBuilder().list();
        }

        public void delete(WanBeanTwo twoBean){
            if (isHas(twoBean)){
                wanBeanTwoDao.delete(twoBean);
            }
        }
        private static boolean isHas(WanBeanTwo wanBeanTwo) {
            List<WanBeanTwo> list = wanBeanTwoDao.queryBuilder().where(WanBeanTwoDao.Properties.Title.eq(wanBeanTwo.getTitle())).list();
            if (list!=null&&list.size() > 0 ){
                return  true;
            }
            return  false;
        }
}
